package server;

import java.io.Serializable;

/**
 * Defines a single rule that the firewall supports.
 */
public class Rule implements Serializable {
	private static final long serialVersionUID = 3307454235662621656L;

	/**
	 * Define an IP rule. Please set NULL for parameters which mean ANY.
	 * @param index The rule's index number.
	 * @param iprange The IP Range for the rule.
	 * @param port The port to check for the rule.
	 * @param permission The permission of the rule.
	 */
	public Rule(int index, String iprange, Integer port, permissionOpt permission) {
		type = typeOpt.IP;
		this.index = index;
		this.iprange = iprange;
		this.port = port;
		this.permission = permission;
	}

	/**
	 * Define an Context rule. Please set NULL for parameters which mean ANY.
	 * @param index The rule's index number.
	 * @param port The port to check for the rule.
	 * @param protocol The name of the protocol to check for.
	 * @param permission The permission of the rule.
	 */
	public Rule(int index, Integer port, String protocol, permissionOpt permission) {
		type = typeOpt.CONTEXT;
		this.index = index;
		this.port = port;
		this.protocol = protocol;
		this.permission = permission;
	}

	public static enum typeOpt {IP, CONTEXT};
	public static enum permissionOpt {ALLOW, DENY};
	public int index;
	public typeOpt type;
	public String iprange = null;
	public Integer port = null;
	public String protocol = null;
	public permissionOpt permission = null;

    public boolean equals(Object other) {
        if (!(other instanceof Rule)) {
            return false;
        }

        Rule o = (Rule)other;
        return (
            (((iprange == null) && (o.iprange == null)) || 
                (o.iprange.equals(iprange))) &&
			(o.type.equals(type)) &&
			(o.index == index) &&
            (((protocol == null) && (o.protocol == null)) || 
                (o.protocol.equals(protocol))) &&
            (((port == null) && (o.port == null)) || 
                (o.port.equals(port))) ||
            (((permission == null) && (o.permission == null)) || 
                (o.permission.equals(permission)))
            );
    }

}
